﻿using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using S.Common.Enums.PregnancyTest;
using S.Common.helper;
using S.PregnancyTest.Read.API.Application.Command;
using Yitter.IdGenerator;

namespace S.PregnancyTest.Read.API.Controllers
{
    /// <summary>
    /// 种母妊检显示查询
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PregnancyTestController : BaseController
    {
        private readonly IMediator mediator;

        private readonly IConfiguration configuration;

        private readonly IHttpContextAccessor httpContextAccessor;

        public PregnancyTestController(IMediator _mediator, MySqlDbContext db, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(db)
        {
            mediator = _mediator;
            this.configuration = configuration;
            this.httpContextAccessor = httpContextAccessor;
        }

        [Authorize]
        [HttpGet("Name")]
        public IActionResult Name()
        {
            return Ok(UserStrategyDto.UserTrueName);
        }
        /// <summary>
        /// 测试apollo
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ApolloText(string key)
        {
            return Ok(configuration.GetValue<string>(key));
        }

        /// <summary>
        /// 将妊检结果枚举转为数组
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult PregnancytestResultEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<PregnancytestResultEnum>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }

        /// <summary>
        /// 将同胎数枚举转为数组
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult ParityNumberEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<ParityNumberEnum>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }

        /// <summary>
        /// 将妊检方式枚举转为数组
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult PregnancyTestTypeEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<PregnancyTestTypeEnum>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }


        /// <summary>
        /// 种母妊检查询
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> PregnancyTestModelList(GetPregnancyTestListCommand comm)
        {
            var list = await mediator.Send(comm);
            list.StatusCode = 200;
            list.Message = "success";
            list.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            return Ok(list);
        }

        /// <summary>
        /// 雪花Id显示
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetYitIdHelper()
        {
            return Ok(YitIdHelper.NextId());
        }

        /// <summary>
        /// 妊检怀孕的耳号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetEartagList()
        {
            return Ok(await mediator.Send(new GetEarTagListCommand() { }));
        }

        /// <summary>
        /// 根据耳号反填信息
        /// </summary>
        /// <param name="EarTag"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<IActionResult> GetPregnancyTestByEarTag(string EarTag)
        {
            return Ok(await mediator.Send(new GetPregnancyTestByEarTagCommand() { EarTag = EarTag }));
        }


        /// <summary>
        /// 跨服务请求栋舍
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetFieldModel()
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/Authorization/api/ColumnManagement/GetColumnList");
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }

        /// <summary>
        /// 跨服务请求栏位
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetHouseField(int id)
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/Authorization/api/ColumnManagement/GetHouseField?id={id}");
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }

        /// <summary>
        /// 跨服务请求负责人信息
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetUser()
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/Authorization/api/UserRead/GetUser");
            // 反序列化并显示负责人数据
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }

        /// <summary>
        /// 跨服务请求配种耳号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetEarTag()
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/ReadBreed/api/ReadMaternalMating/GetBreedEartag");
            // 反序列化并显示耳号数据
            var response = JsonConvert.DeserializeObject<List<SelectDto>>(jsonString);
            return Ok(response);
        }

        /// <summary>
        /// 跨服务请求转舍类型
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetTransFerEnum()
        {
            var jsonString = HttpClientHelper.GetResponse($"{configuration.GetValue<string>("Gateway")}/Authorization/api/ColumnManagement/GetTransFerEnum");
            var response = JsonConvert.DeserializeObject<List<TransFer>>(jsonString);
            return Ok(response);
        }


        public class TransFer
        {
            /// <summary>
            /// 编号
            /// </summary>
            public int value { get; set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string? name { get; set; }
        }



        public class SelectDto
        {
            /// <summary>
            /// 编号
            /// </summary>
            public int value { get; set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string? label { get; set; }
        }

        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TestGet()
        {
            var cfRt = configuration.GetValue<string>("consullp");//读取Consul路由
            return Ok(cfRt);
        }


    }
}
